//
//  SAPAudioTwoViewController.m
//  CarsHome
//
//  Created by dapeng on 15/12/16.
//  Copyright © 2015年 dapeng. All rights reserved.
//

#import "SAPAudioTwoViewController.h"
#import "SAPPlayerViewController.h"
#import "SAPTwoAudioTableViewCell.h"
@interface SAPAudioTwoViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSArray *dataSource;
@end

@implementation SAPAudioTwoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.navigationItem.title = self.name;
    self.dataSource = [[NSArray alloc] init];
    [SAPNetWorkTool getWithUrl:_string parameter:nil httpHeader:nil responseType:ResponseTypeJSON   success:^(id result) {
        NSDictionary *dic = [result valueForKey:@"result"];
        self.oneModel = [[SAPAudioOneModel alloc] initWithDataSource:dic];
        NSMutableArray * arr = [[NSMutableArray alloc] init];
        for (int i = 0; i < self.oneModel.listArray.count; i++) {
            [arr addObject:self.oneModel.listArray[i]];
        }self.dataSource = [NSArray arrayWithArray:arr];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    } fail:^(NSError *error) {
        NSLog(@"%@", error);
    }];
    [self tableViews];

}
- (void)tableViews {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, SCREEN_SIZE.height) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[SAPTwoAudioTableViewCell class] forCellReuseIdentifier:@"SAPTwoAudioTableViewCellIndentifier"];
    [self.tableView registerClass:[SAPAudioTwoTableViewCell class] forCellReuseIdentifier:@"SAPAudioTwoTableViewCellIndentifier"];
    [self.view addSubview:self.tableView];
    [_tableView release];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    
    if (indexPath.section == 0) {
        SAPTwoAudioTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SAPTwoAudioTableViewCellIndentifier"];
        cell.album_name = self.name;
        cell.album_imgurl = self.album_imgurl;
        cell.album_descrition = self.descrition;
        cell.album_bigImgurl = self.album_bigImgurl;
        
        cell.userInteractionEnabled = NO;
        return cell;
    }
    if (indexPath.section == 1) {
        SAPAudioTwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SAPAudioTwoTableViewCellIndentifier"];
        SAPAudioTwoModel *twoModel = self.oneModel.listArray[indexPath.row];
        cell.twoModel = twoModel;
        return cell;
    }
    return nil;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }else {
        return self.oneModel.listArray.count;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return SCREEN_SIZE.height / 4;
    }else {
        return 80;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    }else {
        return 30;
    }
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        return @"按时间";
    }else {
        return nil;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    if (indexPath.section == 1) {
        
    SAPPlayerViewController *playerVC = [SAPPlayerViewController sharePlayerViewController];
    SAPAudioTwoModel *twoModel = self.oneModel.listArray[indexPath.row];
        playerVC.indexRow = indexPath.row;
        playerVC.titles = twoModel.program_title;
        playerVC.string = twoModel.program_fileurl;
        playerVC.titleName.text = twoModel.program_title;
        playerVC.allMusicArray = [NSMutableArray arrayWithArray:self.dataSource] ;
        playerVC.picture = self.album_imgurl;
    [self.navigationController pushViewController:playerVC animated:YES];

    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
